﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bundled.Entity;
using Bundled.ORM;
using System.Data.Objects;
using System.Net.Mail;

namespace Bundled.Pages.PasswordRecovery
{
    public partial class Default1 : System.Web.UI.Page
    {
        BundledDBEntities db;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (db = new BundledDBEntities())
            {
                ObjectResult<Get_PWRecoveryInfo_Result> recoverylResult = db.Get_PWRecoveryInfo(Utility.SanitizeText(txtUsername.Text));
                List<Get_PWRecoveryInfo_Result> recoverylResultList = recoverylResult.ToList();

                if (recoverylResultList != null)
                {
                    foreach (var result in recoverylResultList)
                    {
                        lblQuestion.Text = result.Question;
                    }
                    divUsername.Visible = false;
                    divRecovery.Visible = true;
                    divEmail.Visible = false;
                }
            }
        }

        protected void btnSubmitAnswer_Click(object sender, EventArgs e)
        {
            using (db = new BundledDBEntities())
            {
                ObjectResult<Get_PWRecoveryInfo_Result> recoverylResult = db.Get_PWRecoveryInfo(Utility.SanitizeText(txtUsername.Text));
                List<Get_PWRecoveryInfo_Result> recoverylResultList = recoverylResult.ToList();

                if (recoverylResultList != null)
                {
                    foreach (var result in recoverylResultList)
                    {
                        string answer = Utility.SanitizeText(txtAnswer.Text);
                        if (answer == result.QuestionAnswer)
                        {
                            RecoveryEmail();
                            divUsername.Visible = false;
                            divRecovery.Visible = false;
                            divEmail.Visible = true;
                        }
                    }                  
                }
            }
        }

        protected void RecoveryEmail()
        {
            string email = null;
            string password = null;

            using (db = new BundledDBEntities())
            {
                ObjectResult<Get_PWRecoveryInfo_Result> recoverylResult = db.Get_PWRecoveryInfo(Utility.SanitizeText(txtUsername.Text));
                List<Get_PWRecoveryInfo_Result> recoverylResultList = recoverylResult.ToList();

                if (recoverylResultList != null)
                {
                    foreach (var result in recoverylResultList)
                    {
                        email = result.Email;
                        password = result.Password;
                    }
                }

                string smtpServer = "";

                MailMessage mail = new MailMessage();
                mail.To.Add(email);
                mail.From = new MailAddress("jsl5264@psu.edu");
                mail.Subject = "Password Recovery";
                mail.Body = "Here is your password.\n\n" + "\n\tPassword: " + password;

                smtpServer = System.Web.Configuration.WebConfigurationManager.AppSettings["smtp"];
                SmtpClient mSmtpClient = new SmtpClient(smtpServer);
                mSmtpClient.Send(mail);
            }
        }
    }
}